package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.pay.models.bo.PayUserBankcardSaveBO;
import com.ztgf.pay.models.service.IPayUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.shop.ShopReceiveCardMapper;
import com.ztgf.shopping.models.service.shop.IShopReceiveCardService;
import com.ztgf.shopping.models.bo.shop.ShopReceiveCardSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopReceiveCardDTO;

import java.math.BigDecimal;

/**
 * 收款账号表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-27 14:33:37
 */
@DubboService
public class ShopReceiveCardServiceImpl extends ServiceImpl<ShopReceiveCardMapper, ShopReceiveCardDTO> implements IShopReceiveCardService{

    @Resource
    private YmlConfig ymlConfig;
    @DubboReference
    IPayUserService payUserService;

    @Override
    public boolean add(ShopReceiveCardSaveBO saveBO) {
        //去重
        Wrapper<ShopReceiveCardDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("bank_card_no", saveBO.getBankCardNo());
        wrapper.eq("type", 0);
        wrapper.eq("status", 0);
        ShopReceiveCardDTO shopReceiveCardDTO = this.selectOne(wrapper);

        if (shopReceiveCardDTO != null){
            return false;
        }

        ShopReceiveCardDTO dto = new ShopReceiveCardDTO();
        BeanUtils.copyProperties(saveBO,dto);
        //绑卡
        ShopReceiveCardDTO newDto = bindCard(dto);

        Long id = ymlConfig.getPkId();
        newDto.setId(id);
        return this.insert(dto);
    }
    @Override
    public boolean add1(ShopReceiveCardSaveBO saveBO) {
        //去重
        Wrapper<ShopReceiveCardDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", saveBO.getShopId());
        wrapper.eq("bank_card_no", saveBO.getBankCardNo());
        wrapper.eq("status", 0);
        ShopReceiveCardDTO shopReceiveCardDTO = this.selectOne(wrapper);

        if (shopReceiveCardDTO != null){
            return false;
        }

        ShopReceiveCardDTO dto = new ShopReceiveCardDTO();
        BeanUtils.copyProperties(saveBO,dto);
        //绑卡
        ShopReceiveCardDTO newDto = bindCard1(dto);

        Long id = ymlConfig.getPkId();
        newDto.setId(id);
        return this.insert(dto);
    }

    @Override
    public boolean updateDefault(Long id) {
        ShopReceiveCardDTO shopReceiveCardDTO = this.selectById(id);
        ShopReceiveCardDTO newReceiveCardDTO = new ShopReceiveCardDTO();
        Wrapper<ShopReceiveCardDTO> wrapper = new EntityWrapper<>();
        //获取旧的dto
        ShopReceiveCardDTO oldShopReceiveCardDTO = this.selectOne(wrapper.eq("shop_id", shopReceiveCardDTO.getShopId()).eq("is_default", 1));
        if (oldShopReceiveCardDTO != null){
            ShopReceiveCardDTO oldReceiveCardDTO = new ShopReceiveCardDTO();
            oldReceiveCardDTO.setIsDefault(0);//改为不默认；
            oldReceiveCardDTO.setId(oldShopReceiveCardDTO.getId());
            this.updateById(oldReceiveCardDTO);
        }
        newReceiveCardDTO.setIsDefault(1);//改为默认
        newReceiveCardDTO.setId(id);
        return this.updateById(newReceiveCardDTO);
    }

    @Override
    public ShopReceiveCardDTO getOpenByShopId(Long shopId) {
        Wrapper<ShopReceiveCardDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", shopId).eq("type", 1);
        return this.selectOne(wrapper);
    }

    @Override
    public Result activeCard(Long id, BigDecimal money) {
        ShopReceiveCardDTO dto = this.selectById(id);
        ShopReceiveCardDTO newDto = new ShopReceiveCardDTO();
        newDto.setId(id);
        Result result = payUserService.saveActivationBankCard(dto.getShopId(), 1, dto.getRequestSerial(), money);
        if (result.getCode() == 0){
            //激活成功
            newDto.setActiveStatus(1);
            newDto.setActiveNum(0);
            this.updateById(newDto);
            return new Result<>();
        }else {
            //激活失败
            newDto.setActiveStatus(0);
            int activeNum = dto.getActiveNum() + 1;
            newDto.setActiveNum(activeNum);
            this.updateById(newDto);
            return new Result<>(CodeEnum.FAIL_200017);
        }
    }
    @Override
    public Result activeCardAdmin(Long id, BigDecimal money) {
        ShopReceiveCardDTO dto = this.selectById(id);
        ShopReceiveCardDTO newDto = new ShopReceiveCardDTO();
        newDto.setId(id);
        Result result = payUserService.saveActivationBankCard(dto.getShopId(), 3, dto.getRequestSerial(), money);
        if (result.getCode() == 0){
            //激活成功
            newDto.setActiveStatus(1);
            newDto.setActiveNum(0);
            this.updateById(newDto);
            return new Result<>();
        }else {
            //激活失败
            newDto.setActiveStatus(0);
            int activeNum = dto.getActiveNum() + 1;
            newDto.setActiveNum(activeNum);
            this.updateById(newDto);
            return new Result<>(CodeEnum.FAIL_200017);
        }
    }
    @Override
    public Result goActiveCard(Long id) {
        ShopReceiveCardDTO dto = this.selectById(id);
        int activeNum = dto.getActiveNum();
        if (activeNum >=3 ){
            ShopReceiveCardDTO newDto = new ShopReceiveCardDTO();
            //重新绑卡
            ShopReceiveCardDTO bindDto = bindCard(dto);
            newDto.setId(id);
            newDto.setRequestSerial(bindDto.getRequestSerial());
            newDto.setBindStatus(bindDto.getBindStatus());
            newDto.setActiveNum(0);
            this.updateById(newDto);
            if (newDto.getBindStatus() == 1){
                Result result = new Result<>();
                result.setData(CodeEnum.ACTIVE_TIPS.getMsg());
                return result;
            }else {
                return new Result<>(CodeEnum.FAIL_100007);
            }
        }
        return new Result<>();
    }

    @Override
    public Result goActiveCardAdmin(Long id) {
        ShopReceiveCardDTO dto = this.selectById(id);
        int activeNum = dto.getActiveNum();
        if (activeNum >=3 ){
            ShopReceiveCardDTO newDto = new ShopReceiveCardDTO();
            //重新绑卡
            ShopReceiveCardDTO bindDto = bindCard1(dto);
            newDto.setId(id);
            newDto.setRequestSerial(bindDto.getRequestSerial());
            newDto.setBindStatus(bindDto.getBindStatus());
            newDto.setActiveNum(0);
            this.updateById(newDto);
            if (newDto.getBindStatus() == 1){
                Result result = new Result<>();
                result.setData(CodeEnum.ACTIVE_TIPS.getMsg());
                return result;
            }else {
                return new Result<>(CodeEnum.FAIL_100007);
            }
        }
        return new Result<>();
    }

    @Override
    public boolean deleteCard(Long id) {
        ShopReceiveCardDTO dto = this.selectById(id);
        Result result = payUserService.delBankCard(dto.getShopId(), 1,  dto.getBankCardNo());
        if (result.getCode() == 0){
            return this.deleteById(id);
        }
        return false;
    }

    @Override
    public boolean deleteCardAdmin(Long id) {
        ShopReceiveCardDTO dto = this.selectById(id);
        Result result = payUserService.delBankCard(dto.getShopId(), 3,  dto.getBankCardNo());
        if (result.getCode() == 0){
            return this.deleteById(id);
        }
        return false;
    }

    @Override
    public boolean updateBindStatus(Long id) {
        ShopReceiveCardDTO dto = this.selectById(id);
        ShopReceiveCardDTO newDto = bindCard(dto);
        newDto.setId(id);
        this.updateById(dto);
        return dto.getBindStatus().equals(1);
    }
    @Override
    public boolean updateBindStatusAdmin(Long id) {
        ShopReceiveCardDTO dto = this.selectById(id);
        ShopReceiveCardDTO newDto = bindCard1(dto);
        newDto.setId(id);
        this.updateById(dto);
        return dto.getBindStatus().equals(1);
    }

    private ShopReceiveCardDTO bindCard(ShopReceiveCardDTO dto){
        Long requestSerial = ymlConfig.getPkId(); //请求流水号
        dto.setRequestSerial(requestSerial);

        PayUserBankcardSaveBO bo = new PayUserBankcardSaveBO();
        bo.setId(requestSerial);
        bo.setUserId(dto.getShopId());
        bo.setBankId(dto.getBankId());
        bo.setAccountBankNo(dto.getAccountBankNo());
        bo.setBankCard(dto.getBankCardNo());
        bo.setMobile(dto.getMobile());
        bo.setAccountType(1);
        bo.setCredentialsType(dto.getCredentialsType());
        bo.setCredentialsNo(dto.getCredentialsNo());

        Result result = payUserService.addBankCard(bo);
        if (result.getCode() == 0 || result.getCode() == CodeEnum.FAIL_200014.getCode()){
            //绑卡成功
            dto.setBindStatus(1);
        }else {
            dto.setBindStatus(0);
        }

        return dto;
    }
    private ShopReceiveCardDTO bindCard1(ShopReceiveCardDTO dto){
        Long requestSerial = ymlConfig.getPkId(); //请求流水号
        dto.setRequestSerial(requestSerial);

        PayUserBankcardSaveBO bo = new PayUserBankcardSaveBO();
        bo.setId(requestSerial);
        bo.setUserId(dto.getShopId());
        bo.setBankId(dto.getBankId());
        bo.setAccountBankNo(dto.getAccountBankNo());
        bo.setBankCard(dto.getBankCardNo());
        bo.setMobile(dto.getMobile());
        bo.setAccountType(3);
        bo.setCredentialsType(dto.getCredentialsType());
        bo.setCredentialsNo(dto.getCredentialsNo());

        Result result = payUserService.addBankCard(bo);
        if (result.getCode() == 0 || result.getCode() == CodeEnum.FAIL_200014.getCode()){
            //绑卡成功
            dto.setBindStatus(1);
        }else {
            dto.setBindStatus(0);
        }

        return dto;
    }
}
